//
//  MeatShopHeader.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/19.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatShopHeader.h"

@interface MeatShopHeader ()

/**
 MeatShopHeader 's LeftNavBtn -> UIButton
 */
@property (strong, nonatomic) UIButton *leftNavBtn;


/**
 MeatShopHeader 's shopNameLab -> UIlabel
 */
@property (nonatomic, strong) UILabel *shopNameLab;



/**
 MeatShopHeader 's shopDetailLab -> UIlabel
 */
@property (nonatomic, strong) UILabel *shopDetailLab;



/**
 MeatShopHeader 's shopIcon -> UIImageView
 */
@property (nonatomic, strong) UIImageView *shopIcon;


/**
 MeatShopHeader 's messageBtn -> UIButton
 */
@property (nonatomic, strong) UIButton *messageBtn;



/**
 MeatShopHeader 's icons -> NSArray
 */
@property (nonatomic, strong) NSArray *icons;


/**
 MeatShopHeader 's titles -> NSArray
 */
@property (nonatomic, strong) NSArray *titles;


/**
 MeatShopHeader 's titleVaules -> NSArray
 */
@property (nonatomic, strong) NSArray *titleVaules;


@end

@implementation MeatShopHeader


- (void)setupUI
{
    UIImage *image = [UIImage imageNamed:@"meat_shop_bg"];
    self.layer.contents = (__bridge id)image.CGImage;
    
    [self addSubview:self.leftNavBtn];
    [self addSubview:self.shopIcon];
    [self addSubview:self.shopNameLab];
    [self addSubview:self.shopDetailLab];
    [self addSubview:self.messageBtn];
    
    [self addList];
    [self addTitleList];
}

- (void)addList{
    
    __block CGFloat gap = 40.f;
    @weakify(self);
    [self.icons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        @strongify(self);
        
        UIImageView *icon = [UIImageView makeImageByImageName:obj cornerRadius:0];
        icon.size = CGSizeMake(40, 40 );
        icon.mj_y = TJ_StatusBarHeight ;
        icon.mj_x = KScreenWidth - 3*gap +gap*idx;
        icon.isNeedAnimation = YES;
        [self addSubview:icon];
        
        
        icon.tapBlock = ^{
            
            if (self.rightListBlock) {
                self.rightListBlock(idx);
            }
        };
    }];
}


- (void)addTitleList{
    
    __block CGFloat gap = KScreenWidth / 3.f;
    @weakify(self);
    [self.titleVaules enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        @strongify(self);
        UILabel *valueLab = [UILabel makeLabelWithText:obj textColor:KWhiteColor font:mFont(15) textAliment:1];
        valueLab.frame = CGRectMake(gap *idx, TJ_StatusBarHeight + 110, gap, 18);
        
        UILabel *titleLab = [UILabel makeLabelWithText:self.titles[idx] textColor:KWhiteColor font:mFont(12) textAliment:1];
        titleLab.frame = CGRectMake(gap *idx, valueLab.bottom + 5, gap, 18);
        
        [self addSubview:valueLab];
        [self addSubview:titleLab];
        
        if (idx >0) {
            CAShapeLayer *line = [CAShapeLayer layer];
            line.fillColor = UIColorFromRGB(0x919191).CGColor;
            UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(gap *(idx), TJ_StatusBarHeight + 120, 1, 20)];
            line.path = path.CGPath;
            [self.layer addSublayer:line];
        }

    }];

}


- (void)setupLayout
{
    self.height = TJ_MeatShop_Header;
    CGFloat topHeight = TJ_ISIphoneX ?34 :20;

    {
        _leftNavBtn.frame = CGRectMake([self fitSize:15], topHeight, 44,  TJ_NavBarHeight-topHeight);
    }
    
    {
        _shopIcon.size = CGSizeMake(40, 40);
        _shopIcon.mj_x = 15;
        _shopIcon.mj_y = _leftNavBtn.bottom + 10;
    }
    
    {
        _shopNameLab.mj_x = _shopIcon.right + 10;
        _shopNameLab.mj_y = _shopIcon.mj_y;
        _shopNameLab.size = CGSizeMake(KScreenWidth - _shopNameLab.left - 80, 21);
    }

    
    {
        _shopDetailLab.size = CGSizeMake(_shopNameLab.width, 18);
        _shopDetailLab.mj_x = _shopIcon.right + 10;
        _shopDetailLab.mj_y = _shopNameLab.bottom + 1;
    }

    
    {
        _messageBtn.mj_y = _shopIcon.mj_y + 10;
        _messageBtn.size = CGSizeMake(56, 27);
        _messageBtn.mj_x = self.right - _messageBtn.width -15;

    }
}



#pragma mark - response methods
- (void)didClickedBackButton{
    
    if (self.leftNavBlock) {
        self.leftNavBlock();
    }
}

#pragma mark - setter and getter

- (UIImageView *)shopIcon{
    
    if (!_shopIcon) {
        _shopIcon = [[UIImageView alloc] init];
        _shopIcon.image = [UIImage imageNamed:@"meat_shop_Icon"];
        [self addShadowAndCircleCorner:_shopIcon.layer corner:8];
    }
    return _shopIcon;
}


- (UILabel *)shopNameLab{
    
    if (!_shopNameLab) {
        _shopNameLab = [UILabel makeLabelWithText:@"石头剪刀铺" textColor:KWhiteColor font:mFont(15) textAliment:0];
    }
    return _shopNameLab;
}


- (UILabel *)shopDetailLab{
    
    if (!_shopDetailLab) {
        _shopDetailLab = [UILabel makeLabelWithText:@"整猪转让，电话请联系18519283443" textColor:UIColorFromRGB(0xF2F2F2) font:mFont(11) textAliment:0];
    }
    return _shopDetailLab;
}


- (UIButton *)leftNavBtn{
    
    if (!_leftNavBtn) {
        _leftNavBtn = [UIButton makeButtonWithImageName:@"meat_shop_nav_white"];
        _leftNavBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        kWeakSelf(self);
        _leftNavBtn.tapBlock = ^{
            if (weakself.leftNavBlock) {
                weakself.leftNavBlock();
            }
        };
    }
    
    return _leftNavBtn;
}


- (UIButton *)messageBtn{
    
    if (!_messageBtn) {
        _messageBtn = [UIButton makeButtonWithText:@"聊一聊" textColor:KWhiteColor font:mFont(12) textAliment:1 cornerRadius:5];
        _messageBtn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5f];
        _messageBtn.layer.borderWidth = .5f;
        _messageBtn.layer.borderColor = KWhiteColor.CGColor;
        
        kWeakSelf(self);
        _messageBtn.tapBlock = ^{
            if (weakself.leftNavBlock) {
                weakself.leftNavBlock();
            }
        };
    }
    
    return _messageBtn;
}


- (NSArray *)icons{
    
    if (!_icons) {
        _icons = @[@"meat_shop_heart", @"meat_shop_collection_light", @"meat_shop_more"];
    }
    return _icons;
}


- (NSArray *)titles{
    
    if (!_titles) {
        _titles = @[@"收藏", @"点赞", @"评价"];
    }
    return _titles;
}


- (NSArray *)titleVaules{
    
    if (!_titleVaules) {
        _titleVaules = @[@"26", @"99", @"89"];
    }
    return _titleVaules;
}


@end
